﻿#region Header

/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion Header

namespace Probel.Ldoc.ViewModels.Controls
{
    using System.Windows.Input;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;
    using Probel.Ldoc.ViewModels.Pages;
    using Probel.Ldoc.ViewModels.Properties;
    using Probel.Mvvm.DataBinding;
    using Probel.Mvvm.Gui;

    public class ProfessionPopupViewModel : BaseViewModel, ITextPopupViewModel
    {
        #region Fields

        private readonly ICommand createProfessionCommand;
        private readonly PatientEditorViewModel ParentViewModel;
        private readonly PatientCardDto Patient;
        private readonly IPatientService Service;

        private string professionName;

        #endregion Fields

        #region Constructors

        public ProfessionPopupViewModel(PatientEditorViewModel parent, IErrorService errorService, IPatientService service, PatientCardDto patient, bool isAsync = true)
            : base(errorService, isAsync)
        {
            this.ParentViewModel = parent;
            this.Service = service;
            this.Patient = patient;

            this.createProfessionCommand = new RelayCommand(this.Create, this.CanCreate);
        }

        #endregion Constructors

        #region Properties

        public ICommand CreateCommand
        {
            get { return this.createProfessionCommand; }
        }

        public string ItemName
        {
            get { return this.professionName; }
            set
            {
                this.professionName = value;
                this.OnPropertyChanged(() => ItemName);
            }
        }

        public string Watermark
        {
            get { return Messages.Watermark_NewProfession; }
        }

        #endregion Properties

        #region Methods

        private bool CanCreate()
        {
            return !string.IsNullOrWhiteSpace(this.ItemName); ;
        }

        private void Create()
        {
            var profession = new ProfessionCardDto() { Name = this.ItemName };
            if (!this.Service.Exists(profession))
            {
                this.Service.CreateAndBind(profession, this.Patient);
                this.ParentViewModel.RefreshProfession();
            }
            else
            {
                this.Service.Bind(profession, this.Patient);
                this.ParentViewModel.RefreshProfession();
                //ViewService.MessageBox.Information(Messages.Msg_ProfessionExists);
                this.ParentViewModel.UiMessage = UiMessageDto.Warn(Messages.Msg_ProfessionExists);
            }
        }

        #endregion Methods
    }
}